package cn.yunhe.servlet;

import cn.yunhe.entity.News;
import cn.yunhe.entity.Product;
import cn.yunhe.entity.ProductCategory;
import cn.yunhe.service.NewsService;
import cn.yunhe.service.ProductCategoryService;
import cn.yunhe.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/ToUpdateNewsServlet")
public class ToUpdateNewsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          int id = Integer.parseInt(request.getParameter("enid"));
        System.out.println(id);
          News news = new News();
          NewsService newsService = new NewsService();
          news = newsService.getNewsById(id);
        ProductCategoryService pcs = new ProductCategoryService();
        List<ProductCategory> list = pcs.getParents();
        ProductService productService = new ProductService();
        Product product = productService.getById(news.getReferproduct());
        ProductCategory ChildClass = pcs.getCategoryById(news.getChildClass());
        request.setAttribute("product",product);
        request.setAttribute("ChildClass",ChildClass);
          request.setAttribute("list", list);
          request.setAttribute("news",news);

          request.getRequestDispatcher("manage/news-modify.jsp").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
